package cn.com.backstage.controller.base;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_enums;
import cn.com.api.entity.ApiT_member_entrepreneur;
import cn.com.api.entity.ApiU_advert;
import cn.com.api.entity.ApiU_advert_position;
import cn.com.api.entity.U_label;
import cn.com.api.service.ApiT_enumsService;
import cn.com.api.service.ApiT_member_entrepreneurService;
import cn.com.api.service.ApiU_advertService;
import cn.com.api.service.ApiU_advert_positionService;
import cn.com.api.service.U_labelService;
import cn.com.util.*;

public class U_advertBaseController {

	private static Logger logger = Logger.getLogger(U_advertBaseController.class);

	@Autowired
	private ApiU_advertService apiU_advertService;
	@Autowired
	private ApiU_advert_positionService apiU_advert_positionService;
	@Autowired
	private ApiT_enumsService apiT_enumsService;
	@Autowired
	private ApiT_member_entrepreneurService apiT_member_entrepreneurService;
	@Autowired
	private U_labelService labelService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_advert-list")
	public ModelAndView u_advert_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_advert_list.jsp");

		List<ApiU_advert> u_adverts = apiU_advertService.findU_adverts();
		mv.addObject("u_adverts", u_adverts);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_advert-add-pre")
	public ModelAndView u_advert_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_advert_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiU_advert u_advert = apiU_advertService.findU_advertById(Integer.valueOf(id));
			mv.addObject("u_advert", u_advert);
		}
		// 年龄区间字典
		List<ApiT_enums> agerange = apiT_enumsService.findT_enumssByItem_key("agerange");
		List<ApiT_enums> ageranges = apiT_enumsService.findT_enumssByParent_id(agerange.get(0).getId() + "");
		mv.addObject("ageranges", ageranges);

		// 职业字典
		List<ApiT_enums> profession = apiT_enumsService.findT_enumssByItem_key("profession");
		List<ApiT_enums> professions = apiT_enumsService.findT_enumssByParent_id(profession.get(0).getId() + "");
		mv.addObject("professions", professions);

		List<ApiU_advert_position> u_advert_positions = apiU_advert_positionService.findU_advert_positions();
		mv.addObject("u_advert_positions", u_advert_positions);

		// 回去兴趣爱好标签库
		List<U_label> alllabels = labelService.getAlllabels(1);
		mv.addObject("alllabels", alllabels);

		// 获取企业主信息
		Map paramMap = new HashMap();
		paramMap.put("status", 1);
		PageInfo page = new PageInfo();
		page.setCurrentPage(1);
		page.setShowCount(5);
		List<ApiT_member_entrepreneur> entrepreneurs = apiT_member_entrepreneurService
				.findT_member_entrepreneursByMap(paramMap, page);
		mv.addObject("entrepreneurs", entrepreneurs);

		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_advert-add")
	public void u_advert_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String adv_name = request.getParameter("adv_name");
		String adv_desc = request.getParameter("adv_desc");
		String zan_count = request.getParameter("zan_count");
		String position_id = request.getParameter("position_id");
		String add_time = request.getParameter("add_time");
		String ent_id = request.getParameter("ent_id");
		String add_uid = request.getParameter("add_uid");
		String resource_type = request.getParameter("resource_type");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String total_amount = request.getParameter("total_amount");
		String age_range = request.getParameter("age_range");
		String job_title = request.getParameter("job_title");
		String interest_key = request.getParameter("interest_key");
		String logo = request.getParameter("logo");
		String latitude = request.getParameter("latitude");
		String longitude = request.getParameter("longitude");
		String show_times = request.getParameter("show_times");
		String bonus_id = request.getParameter("bonus_id");
		String ext1 = request.getParameter("ext1");
		String ext2 = request.getParameter("ext2");
		String ext3 = request.getParameter("ext3");
		String ext4 = request.getParameter("ext4");
		String ext5 = request.getParameter("ext5");
		String sort = request.getParameter("sort");
		String area = request.getParameter("area");
		String detail_url = request.getParameter("detail_url");
		String distance_range = request.getParameter("distance_range");
		String start_time = request.getParameter("start_time");
		String end_time = request.getParameter("end_time");
		String status = request.getParameter("status");
		String address = request.getParameter("address");
		String covers = request.getParameter("covers");

		Map<String, Object> map = new HashMap<String, Object>();
		// String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiU_advert u_advert = new ApiU_advert();
			// u_advert.setId(id);
			u_advert.setAdv_name(adv_name);
			u_advert.setAdv_desc(adv_desc);
			u_advert.setPosition_id(Integer.valueOf(position_id));
			u_advert.setAdd_time(DateUtil.nowTimeToString());
			u_advert.setEnt_id(ent_id);
			u_advert.setAdd_uid(add_uid);
			u_advert.setResource_type(Integer.valueOf(resource_type));
			u_advert.setProvince(province);
			u_advert.setCity(city);
			u_advert.setArea(area);
			u_advert.setTotal_amount(new BigDecimal(total_amount));
			u_advert.setAge_range(age_range);
			u_advert.setJob_title(job_title);
			u_advert.setInterest_key(interest_key);
			u_advert.setLogo(logo);
			u_advert.setLatitude(latitude);
			u_advert.setLongitude(longitude);
			// u_advert.setShow_times(show_times);
			u_advert.setBonus_id(bonus_id);
			u_advert.setExt1(ext1);
			u_advert.setExt2(ext2);
			u_advert.setExt3(ext3);
			u_advert.setExt4(ext4);
			u_advert.setExt5(ext5);
			u_advert.setSort(Integer.valueOf(sort));

			u_advert.setDetail_url(detail_url);
			u_advert.setDistance_range(Long.valueOf(distance_range));
			u_advert.setStart_time(start_time);
			u_advert.setEnd_time(end_time);
			u_advert.setStatus(1);
			u_advert.setAddress(address);
			u_advert.setCovers(covers);

			apiU_advertService.insertU_advert(u_advert);
		}
		// edit
		else {
			ApiU_advert u_advert = apiU_advertService.findU_advertById(Integer.valueOf(id));
			/*
			 * u_advert.setId(id); u_advert.setAdv_name(adv_name);
			 * u_advert.setAdv_desc(adv_desc); u_advert.setZan_count(zan_count);
			 * u_advert.setAdv_type(adv_type); u_advert.setAdd_time(add_time);
			 * u_advert.setEnt_id(ent_id); u_advert.setAdd_uid(add_uid);
			 * u_advert.setResource_type(resource_type);
			 * u_advert.setProvince(province); u_advert.setCity(city);
			 * u_advert.setTotal_amount(total_amount);
			 * u_advert.setAge_range(age_range);
			 * u_advert.setJob_title(job_title);
			 * u_advert.setInterest_key(interest_key); u_advert.setLogo(logo);
			 * u_advert.setLatitude(latitude); u_advert.setLongitude(longitude);
			 * u_advert.setShow_times(show_times);
			 * u_advert.setBonus_id(bonus_id); u_advert.setExt1(ext1);
			 * u_advert.setExt2(ext2); u_advert.setExt3(ext3);
			 * u_advert.setExt4(ext4); u_advert.setExt5(ext5);
			 * u_advert.setSort(sort); u_advert.setArea(area);
			 * u_advert.setDetail_url(detail_url);
			 * u_advert.setDistance_range(distance_range);
			 * u_advert.setUnit_price(unit_price);
			 * u_advert.setStart_time(start_time);
			 * u_advert.setEnd_time(end_time); u_advert.setDay_price(day_price);
			 * u_advert.setStatus(status); u_advert.setAddress(address);
			 * u_advert.setCovers(covers);
			 */
			apiU_advertService.updateU_advert(u_advert);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_advert-delete")
	public void u_advert_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiU_advertService.deleteU_advert(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
